<template>
    <div>
        <!-- 面包屑导航 -->
        <Bread></Bread>
        <!-- card卡片 -->
        <el-card>
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-input placeholder="请输入内容" class="input-with-select" v-model="query.query">
                        <el-button slot="append" icon="el-icon-search" @click="getUser"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="8">
                    <el-button type="primary" @click="dialogFormVisible = true">添加用户</el-button>
                </el-col>
            </el-row>
            <!-- 表格 -->
            <el-table :data="tableData" border row-key="id">
                <el-table-column type="index" label="#" width="60">
                </el-table-column>
                <el-table-column prop="username" label="姓名">
                </el-table-column>
                <el-table-column prop="email" label="邮箱">
                </el-table-column>
                <el-table-column prop="mobile" label="电话">
                </el-table-column>
                <el-table-column prop="role_name" label="角色">
                </el-table-column>
                <el-table-column prop="mg_state" label="状态">
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.mg_state" active-color="#13ce66" inactive-color="#ff4949"
                            @change="flag(scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column prop="address" label="操作" width="300">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="editId(scope.row.id)">编辑
                        </el-button>
                        <!-- 删除按钮 -->
                        <el-button type="success" icon="el-icon-delete" size="mini" @click="del(scope.row.id)">删除
                        </el-button>
                        <el-button type="info" icon="el-icon-message" size="mini">分配角色</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="query.pagenum" :page-sizes="[2, 3, 4, 5]" :page-size="query.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
            <!-- 添加模态框 -->
            <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
                <el-form :model="form" :rules="rules" ref="reform">
                    <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
                        <el-input v-model="form.username" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
                        <el-input v-model="form.password" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
                        <el-input v-model="form.email" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号" :label-width="formLabelWidth" prop="mobile">
                        <el-input v-model="form.mobile" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="add()">确 定</el-button>
                </div>
            </el-dialog>
            <!-- 编辑模态框 -->
            <el-dialog title="编辑用户" :visible.sync="dialogEdit">
                <el-form :model="editForm" :rules="rules">
                    <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
                        <el-input v-model="editForm.username" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
                        <el-input v-model="editForm.password" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
                        <el-input v-model="editForm.email" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号" :label-width="formLabelWidth" prop="mobile">
                        <el-input v-model="editForm.mobile" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogEdit = false">取 消</el-button>
                    <el-button type="primary" @click="edit()">确 定</el-button>
                </div>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
import Bread from '@/components/Bread.vue'
import qs from 'querystring'
export default {
    components: { Bread },
    data() {
        return {
            tableData: [],
            /* 分页参数 */
            query: {
                query: '',
                pagenum: 1,
                pagesize: 3
            },
            total: 0,
            dialogFormVisible: false,
            dialogEdit: false,
            formLabelWidth: '120px',
            /* 添加表单 */
            form: {
                username: '',
                password: '',
                email: '',
                mobile: ''
            },
            /* 编辑表单 */
            editForm: {},
            /* 表单校验 */
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 6, message: '密码长度最小3位最大6位', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' }
                ],
                mobile: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                ]
            }
        }
    },
    methods: {
        /* 获取用户列表数据 */
        async getUser() {
            let res = await this.$http.get('users?' + qs.stringify(this.query));
            this.tableData = res.data.users
            this.total = res.data.total
            this.query.query = ''
        },
        /* 每页条数发生变化 */
        handleSizeChange(size) {
            this.query.pagesize = size
            this.getUser()
        },
        /* 当前页数据发生改变 */
        handleCurrentChange(nowPage) {
            this.query.pagenum = nowPage
            this.getUser()
        },
        /* 修改状态 */
        async flag(row) {
            // console.log(row);
            let res = await this.$http.put(`/users/${row.id}/state/${row.mg_state}`);
            // console.log(res);
            if (res.meta.status == 200) {
                this.$message.success(res.meta.msg)
            } else {
                this.$message.error(res.meta.msg)
            }
        },
        /* 删除 */
        del(id) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const data = await this.$http.delete("users/" + id);
                console.log(data);
                if (data.meta.status == 200) {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getUser();
                }
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        /* 添加 */
        add() {
            console.log(this.$refs);
            this.$refs.reform.validate(async valid => {
                if (valid) {
                    this.$message.success('表单校验通过');
                    let res = await this.$http.post('users', this.form);
                    console.log(res);
                    if (res.meta.status == 201) {
                        this.$message.success(res.meta.msg);
                        this.dialogFormVisible = false;
                        this.getUser();
                        this.form = {
                            username: '',
                            password: '',
                            email: '',
                            mobile: ''
                        };
                    } else {
                        this.$message.error(res.meta.msg);
                    }
                } else {
                    this.$message.error('表单校验未通过');
                }
            })
        },
        /* 编辑 */
        async editId(id) {
            // console.log(id);
            const res = await this.$http.get('users/' + id);
            // console.log(res);
            if (res.meta.status == 200) {
                this.$message.success(res.meta.msg);
                this.editForm = res.data;
                this.dialogEdit = true;
            } else {
                this.$message.error(res.meta.msg);
            }
        },
        /* 修改内容 */
        async edit() {
            // console.log(this.editForm)
            let res = await this.$http.put('users/' + this.editForm.id, this.editForm);
            // console.log(res);
            if (res.meta.status == 200) {
                this.$message.success(res.meta.msg);
                this.getUser();
                this.dialogEdit = false;
            } else {
                this.$message.error(res.meta.msg);
            }
        }
    },
    mounted() {
        this.getUser();
    },
}
</script>



    






<style lang="scss" >

</style>